﻿#include "pm2_5.h"



QSerialPort *myplotcom = new QSerialPort;
void MainWindow::usartDataInit()
{
//    UsartData::myplotcom = new QSerialPort;
    myplotcom->setBaudRate(QSerialPort::Baud9600,QSerialPort::AllDirections);//设置波特率和读写方向
    myplotcom->setDataBits(QSerialPort::Data8);      //数据位为8位
    myplotcom->setFlowControl(QSerialPort::NoFlowControl);//无流控制
    myplotcom->setParity(QSerialPort::NoParity); //无校验位
    myplotcom->setStopBits(QSerialPort::OneStop); //一位停止位
    #ifdef Q_OS_WIN//如果是windows系统
        myplotcom->setPortName("COM1");//ttymxc2
    #else//如果是unix或者其他系统
          myplotcom->setPortName("ttyS3");//ttymxc2
    #endif

    myplotcom ->open(QIODevice::ReadWrite);

//    connect(myplotcom,SIGNAL(readyRead()),this,SLOT(receiveInfo()));
    myReadTimer=new QTimer(this);
    myReadTimer->setInterval(500);
    connect(myReadTimer,&QTimer::timeout,this,&MainWindow::receiveInfo);//这个定时器是一进此界面就开始启动，定时读取，主要读取探测器参数。

    myReadTimer->start();
}
//接收到单片机发送的数据进行解析
void MainWindow::receiveInfo()
{


    if (myplotcom->bytesAvailable()<=0){return;}
    myHelper::Sleep(100);//延时100毫秒保证接收到的是一条完整的数据,而不是脱节的,
    QByteArray info = myplotcom->readAll();

    QString plotDataHex = myHelper::ByteArrayToHexStr(info);//分出16进制HEX文件打印输出，后期可以关闭
    qDebug() << plotDataHex << endl;

    if(info.length()<24)return;

    if((uchar)info.at(0)==0x32&&(uchar)info.at(1==0x3d))
    {
        PM_2_5_data = (uchar)info.at(6)<<8|(uchar)info.at(7);
        PM10_data = (uchar)info.at(8)<<8|(uchar)info.at(9);

        ui->label_12->setText(QString::number(PM_2_5_data));
        ui->label_13->setText(QString::number(PM10_data));
        if(show_pm2_5)
        {
            now_time++;
            dataCustomPlot->graph(0)->addData(now_time, PM_2_5_data);         //addData(double key, double value);原型
            dataCustomPlot->graph(1)->addData(now_time, PM10_data);

            dataCustomPlot->replot();
        }
    }

}
